package com.freesky.mobile.wechat.controller;

import com.alibaba.fastjson2.JSON;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.mobile.wechat.config.WxCpConfiguration;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpAgentJsapiSignature;
import me.chanjar.weixin.cp.bean.WxCpAgentWorkBench;
import me.chanjar.weixin.cp.bean.workbench.WorkBenchList;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("/wx/jsapi/cp/{agentId}")
public class WxCpJsapiController {


    @RequestMapping("/getJsapiTicket")
    @ResponseBody
    public AjaxResult getJsapiTicket(@PathVariable String agentId,
                                     @RequestParam("url") String url,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws WxErrorException {
        try {
            if(StringUtils.isNotEmpty(url)){
                url = StringEscapeUtils.unescapeHtml4(url);
            }

            final WxCpService wxCpService = WxCpConfiguration.getCpService(Integer.valueOf(agentId));
            WxJsapiSignature wxJsapiSignature = wxCpService.createJsapiSignature(url);
            System.out.println("getJsapiTicket:"+ JSON.toJSONString(wxJsapiSignature));

            return AjaxResult.success("操作成功",wxJsapiSignature);
        } catch (Exception ioe){
            ioe.printStackTrace();
            return AjaxResult.error("系统异常,操作失败!");
        }
    }


    @RequestMapping("/getAgentTicket")
    @ResponseBody
    public AjaxResult getAgentTicket(@PathVariable String agentId,
                                      @RequestParam("url") String url,
                                      HttpServletRequest request,
                                      HttpServletResponse response) throws WxErrorException {
        try {
            if(StringUtils.isNotEmpty(url)){
                url = StringEscapeUtils.unescapeHtml4(url);
            }

            final WxCpService wxCpService = WxCpConfiguration.getCpService(Integer.valueOf(agentId));

            WxCpAgentJsapiSignature wxCpAgentJsapiSignature = wxCpService.createAgentJsapiSignature(url);

            //String agentTicket = wxCpService.getAgentJsapiTicket();
            System.out.println("getAgentTicket:"+ JSON.toJSONString(wxCpAgentJsapiSignature));

            return AjaxResult.success("操作成功",wxCpAgentJsapiSignature);
        } catch (Exception ioe){
            ioe.printStackTrace();
            return AjaxResult.error("系统异常,操作失败!");
        }
    }

    //https://npc.yrjkjs.com/exchange/wx/jsapi/cp/1000007/setWorkbenchTemplate.do
    //http://127.0.0.1:9093/exchange/wx/jsapi/cp/1000007/setWorkbenchTemplate.do
    @RequestMapping("/setWorkbenchTemplate")
    @ResponseBody
    public AjaxResult setWorkbenchTemplate(@PathVariable String agentId,
                                      HttpServletRequest request,
                                      HttpServletResponse response) throws WxErrorException {
        try {

            final WxCpService wxCpService = WxCpConfiguration.getCpService(Integer.valueOf(agentId));

            WxCpAgentWorkBench wxCpAgentWorkBench = new WxCpAgentWorkBench();

            wxCpAgentWorkBench.setAgentId(Long.parseLong(agentId));

            wxCpAgentWorkBench.setType("list");

            List<WorkBenchList> workBenchLists = new ArrayList<>();
            WorkBenchList workBenchList = new WorkBenchList();
            workBenchList.setTitle("产品增持");
            workBenchList.setJumpUrl("https://npc.yrjkjs.com/#/home");
            workBenchLists.add(workBenchList);

            WorkBenchList workBenchList1 = new WorkBenchList();
            workBenchList1.setTitle("购买基金");
            workBenchList1.setJumpUrl("https://npc.yrjkjs.com/#/home");
            workBenchLists.add(workBenchList1);

            WorkBenchList workBenchList3 = new WorkBenchList();
            workBenchList3.setTitle("购买保险");
            workBenchList3.setJumpUrl("https://npc.yrjkjs.com/#/home");
            workBenchLists.add(workBenchList3);

            wxCpAgentWorkBench.setLists(workBenchLists);

            wxCpService.getWorkBenchService().setWorkBenchTemplate(wxCpAgentWorkBench);

            return AjaxResult.success("操作成功");

        } catch (Exception ioe){
            ioe.printStackTrace();
            return AjaxResult.error("系统异常,操作失败!");
        }
    }

}
